package com.itpan.Utils;



import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.itpan.Entity.Product;
import com.itpan.Entity.PurchaseOrder;
import com.itpan.Entity.PurchaseOrderDetail;
import com.itpan.Service.ProductService;
import com.itpan.Service.PurchaseOrderDetailService;
import com.itpan.Service.PurchaseOrderService;

import java.util.ArrayList;
import java.util.List;

public class PurchaseOrderDetailExcelListener extends AnalysisEventListener<PurchaseOrderDetail> {

    private final PurchaseOrderDetailService purchaseOrderDetailService;
    private final  EntityValidate entityValidate;

    private final PurchaseOrderService purchaseOrderService;

    private final ProductService productService;
    private List<PurchaseOrderDetail> purchaseOrderDetailList = new ArrayList<>();

    public PurchaseOrderDetailExcelListener(PurchaseOrderDetailService purchaseOrderDetailService, EntityValidate entityValidate, PurchaseOrderService purchaseOrderService, ProductService productService) {
        this.purchaseOrderDetailService = purchaseOrderDetailService;
        this.entityValidate = entityValidate;
        this.purchaseOrderService = purchaseOrderService;
        this.productService = productService;
    }

    @Override
    public void invoke(PurchaseOrderDetail purchaseOrderDetail, AnalysisContext context) {
        // 校验数据是否有效
        if (isValidPurchaseOrderDetail(purchaseOrderDetail) && !isAlreadyRegistered(purchaseOrderDetail) &&isExistPOPU(purchaseOrderDetail)&& entityValidate.isValidateOfPurchaseOrderDetail(purchaseOrderDetail)) {
            purchaseOrderDetailList.add(purchaseOrderDetail);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 读取结束后的操作，可以进行批量保存或其他处理
        if (!purchaseOrderDetailList.isEmpty()) {
            System.out.println("77");
            purchaseOrderDetailService.saveAll(purchaseOrderDetailList);
        }
    }

    private boolean isValidPurchaseOrderDetail(PurchaseOrderDetail purchaseOrderDetail) {
        // 检查每个字段是否为null或空
        return purchaseOrderDetail.getDetailID() != null
                && purchaseOrderDetail.getOrderID() != null
                && purchaseOrderDetail.getProductID() != null
                && purchaseOrderDetail.getQuantity() != null

                && purchaseOrderDetail.getTotalPrice() != null
                && purchaseOrderDetail.getNotes() != null;
    }

    private boolean isAlreadyRegistered(PurchaseOrderDetail purchaseOrderDetail) {
        PurchaseOrderDetail existingPurchaseOrderDetail = purchaseOrderDetailService.getById(purchaseOrderDetail.getDetailID());
        return existingPurchaseOrderDetail != null;
    }

    private boolean isExistPOPU(PurchaseOrderDetail purchaseOrderDetail){

        //验证商品是否存在
        Product po = productService.getById(purchaseOrderDetail.getProductID());

        //验证主表是否存在
        PurchaseOrder pu = purchaseOrderService.getById(purchaseOrderDetail.getOrderID());

        return (po!=null && pu!=null);

    }
}
